/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package memory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author DAM110
 */
public class GuardarDatos{

    
    private static File f = new File("Jugadores.txt");
    private static FileReader fr;
    private static BufferedReader in;
    private static PrintWriter pw;
        
    public GuardarDatos() {
    }
    
    public static boolean guardarBien(Puntuacion p){
        
        JFileChooser chooser = new JFileChooser("C:\\Users\\dam110\\Desktop\\");        
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt","txt"));
        int seleccion = chooser.showDialog(null, "Elige la ruta");
        
        switch(seleccion) {
        case JFileChooser.APPROVE_OPTION:
            try {
                f = new File(chooser.getSelectedFile().getPath() + "." +  ((FileNameExtensionFilter)chooser.getFileFilter()).getExtensions()[0]);
            
                f.createNewFile();
            
                pw = new PrintWriter(f);
            
            for(Jugador j:p){
                
                pw.println("Jugador: " + j.getNombre() + "  Última puntuación: " + j.getPuntuacion());
            }
            pw.flush();
            } catch (FileNotFoundException fileNotFoundException) {
            } catch (IOException ex) {
                Logger.getLogger(GuardarDatos.class.getName()).log(Level.SEVERE, null, ex);
            }
            pw.close();
        break;

        case JFileChooser.CANCEL_OPTION: 
            JOptionPane.showMessageDialog(null, "No has seleccionado la ruta");
            return false;

        case JFileChooser.ERROR_OPTION:
            return false;
        }
        return true;
    
    }
    
    public static Puntuacion recuperarNombresObject(){
        
        ObjectInputStream entrada;
        Puntuacion p = new Puntuacion();
        try {
            
            File archivo= new File("Jugadores.obj");
            archivo.createNewFile();
            if(archivo.length() < 2){
                
                GuardarDatos.guardarNombresObject(new Puntuacion());
            }
            entrada = new ObjectInputStream(new FileInputStream(archivo));
                
            ArrayList<Jugador> leido = (ArrayList<Jugador>)entrada.readObject();
            
            p.addAll(leido);
            
            
            entrada.close();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GuardarDatos.class.getName()).log(Level.SEVERE, null, ex);
        }catch (FileNotFoundException ex) {
            
            Logger.getLogger(Gestora.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException ex) {
            
            Logger.getLogger(Gestora.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex){
            
            Logger.getLogger(Gestora.class.getName()).log(Level.SEVERE, null, ex);
        }
        return p;
    }
    
    
    
    public static boolean guardarNombres(Puntuacion p){
        
        
        pw = null;
        
        
        try{
            pw = new PrintWriter("Jugadores.txt");
           
            for(Jugador a:p){
               
                pw.println(a.getNombre() + " " + a.getPuntuacion());
            }
            
            pw.flush(); 
                    
        }catch(FileNotFoundException e){
            
            System.out.println(e.getMessage());
            return false;
        }finally{
            
            pw.close();            
        }
        
        return true;
    }
    
    public static boolean guardarNombresObject(Puntuacion p){
        
        
        ObjectOutputStream oos = null;
        
        
        try{
            
            oos = new ObjectOutputStream(new FileOutputStream("Jugadores.obj"));
           
            ArrayList<Jugador> escribir = new ArrayList();
            escribir.addAll(p);
            oos.writeObject(escribir);
            
            
            oos.flush();
            oos.close();
                    
        } catch (IOException ex) {
            Logger.getLogger(GuardarDatos.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            
            try {
                
                oos.close();            
            } catch (IOException ex) {
                Logger.getLogger(GuardarDatos.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
            
        }
        
        return true;
    }
}
